<?php
namespace app\user\controller;

use app\user\model\Apply as ApplyModel;
use app\user\model\Setting;
use think\facade\View;
use hema\wechat\Map;

/**
 * 用户认证申请控制器
 */
class Apply extends Controller
{
    /**
     * 获取申请列表
     */
    public function index()
    {
        $model = new ApplyModel;
        $list = $model->getList(0,$this->user_id);
        return View::fetch('index',compact('list'));
    }

    /**
     * 编辑
     */
    public function detail($id)
    {   

        $model = ApplyModel::detail($id);
        if(!$this->request->isAjax()) {
            return View::fetch('detail', compact('model'));
        }
        //提交动作
        $data = $this->postData('data');
        $data['apply_status'] = 10;//驳回后提交
        if ($model->action($data)) {
            return $this->renderSuccess('修改成功', url('apply/index'));
        }
        $error = $model->getError() ?: '修改失败';
        return $this->renderError($error);      
    }

    /**
     * 代理申请
     */
    public function proxy()
    {
        if($this->user['user']['status']['value'] == 30){
            return $this->renderError('您已经是代理了');
        }
        if (!$this->request->isAjax()) {
            if($apply = (new ApplyModel)->where(['user_id' => $this->user['user']['user_id'], 'apply_mode' => 40])->find()){
                return redirect('/user/apply/detail?id='.$apply['apply_id']);
            }
            return View::fetch();
        }
        $model = new ApplyModel;
        $data = $this->postData('data');
        $data['apply_status'] = 10; //待审核
        $data['apply_mode'] = 40;   //代理认证
        $data['user_id'] = $this->user['user']['user_id'];


        if ($model->action($data)) {
            return $this->renderSuccess('添加成功', url('apply/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 地图
     */
    public function getpoint()
    {
        $map = new Map;
        $location = $map->getIp();//坐标信息
        $values = Setting::getItem('web',0);
        View::layout(false);
        View::assign('wxmap_key', $values['wxmap']);
        View::assign('location', $location);
        return View::fetch();
    }
   
}
